package redisDb

import (
	"fmt"
	"github.com/redis/go-redis/v9"
	"golang.org/x/net/context"
	"social-app/config"
)

var (
	RedisClient *redis.Client
	ctx         = context.Background()
)

func init() {
	RedisClient = redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%s", config.RedisHost, config.RedisPort),
		Password: config.RedisPassword,
	})
	if RedisClient.Ping(ctx).Err() != nil {
		panic("Redis链接失败")
	}
}

func RedisSet(key string, value interface{}) error {
	err := RedisClient.Set(ctx, key, value, 0).Err()
	return err
}

func RedisGet(key string) (string, error) {
	return RedisClient.Get(context.Background(), key).Result()
}
